﻿using HandleSign.DataBase.SignTime;
using HandleSign.Models;
using ServerPA.CompletPort.Util;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace HandleSign.utils
{
    class HandleEvenUtlis
    {

        /// <summary>
        /// 处理单个事件
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        public static SignEven HandleEven(TaskNumModel taskNumModel, SignEven se,  int id, SystemDataSetModel sdsModel, SignTimeDAL stDAL, string CurrentTime)
        {
            #region 处理事件
            //签到人数是否达到初始值
            // 事件为1 ： 正常情况，2: 不符合条件的事件， 0 正在等待处理的事件
            if (se.EvenStatus == 0 && se.Stlist.Count > 0 )//cidychou
            {

                //  3/29  按百分比
                se.EvenStatus = stDAL.getManualSignCount(taskNumModel.taskNumId, taskNumModel.classId, se.Stlist.Count, sdsModel.usecoutn) ? 1 : 2; // 事件已经结束

                //for (int t = 0; t < ds.Stlist.Count; t++)
                //{
                #region 统计签到终端共签到次数

                List<Termenal> terlist = new List<Termenal>();
                //SignEven signeven = (SignEven)ds.Tables["tasknumDt"].Rows[t]["TaskNumEven"];
                List<SignTimeModels> StuList = se.Stlist;//(List<SignTime>)ds.Tables["tasknumDt"].Rows[t]["SignStuList"];
                if (StuList.Count > 0)
                {

                    for (int s = 0; s < StuList.Count; s++)//遍历所有被签到的终端,统计个终端被签到次数
                    {
                        foreach (string SsidScan in StuList[s].SsidList)
                        {

                            int leg = SsidScan.ToString().Length;
                            string[] zd = SsidScan.ToString().Split(',');//.Substring(0, leg - 1)
                            if (zd.Length > 0)
                            {
                                for (int i = 0; i < zd.Length; i++)
                                {
                                    if (terlist.Count == 0)
                                    {
                                        Termenal ter = new Termenal();
                                        ter.termenal = zd[i];
                                        ter.count = 1;
                                        terlist.Add(ter);
                                    }
                                    else
                                    {

                                        if (terlist.Where(u => u.termenal == zd[i]).ToList().Count > 0)
                                        {
                                            terlist.Single(u => u.termenal == zd[i]).count = terlist.Single(u => u.termenal == zd[i]).count + 1;
                                        }
                                        else
                                        {
                                            Termenal ter = new Termenal();
                                            ter.termenal = zd[i];
                                            ter.count = 1;
                                            terlist.Add(ter);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    //排序 并去前N个
                    terlist = terlist.OrderByDescending(u => u.count).Take(sdsModel.chenum).ToList();//按签到次数降序,去前N个有效

                    #endregion

                    //循环签到学生,匹配签到终端判断是否签到成功
                    for (int i = 0; i < StuList.Count; i++)
                    {
                        foreach (string SsidScan in StuList[i].SsidList)
                        {
                            string[] str = terlist.Select(u => u.termenal).ToArray();
                            int ppcount = 0;
                            foreach (string ter in str)
                            {
                                string[] ssidStr =SsidScan.Split(',');
                                foreach (string item in ssidStr)
                                {
                                    if (item == ter)
                                    {
                                        //如果签到终端与签到最多的前N个终端中任何一个匹配,签到成功
                                        ppcount++;
                                    }
                                }
                            }
                            if (ppcount > 0)
                            {
                                StuList[i].scanStatus = 1;
                                break;
                            }
                            //else {
                            //    StuList[i].scanStatus = 6;  //不在课室内
                            //    break;
                            //}
                        }
                    }

                    //查询所在教室
                   // List<TerminalInfo> terminalList = baseInfoModel.terminalList.Where(u => u.terminalId.Contains(terlist[0].termenal)).ToList();

                    //List<ClassInfo> classInfoList = baseInfoModel.classList.Where(u => u.scanTaskNum.Equals(taskNum)).ToList();

                    //se.RoomID = terminalList.Count > 0 ? terminalList[0].roomId : 0;  // 

                    se.Stlist = StuList;
                    //signeven.Stlist = StuList;
                    //se.EvenStatus = 1;

                    //if (se.ClassId == 0)
                    //{
                    //    se.ClassId = classInfoList.Count > 0 ? classInfoList[0].classId2task : 0; //调课提前已获取
                    //    //se.ClassId = (baseInfoModel.classList.Select(u => u.scanTaskNum == taskNum).ToList(A)).;
                    //}

                    string EventNum = taskNumModel.taskNumId + "_" + id;
                    se.EventNum = EventNum;

                    //计算时间阈值
                    string timehold = Convert.ToDateTime(se.Rctime).AddSeconds(-sdsModel.holdtime) + "-" + Convert.ToDateTime(se.Rctime).AddSeconds(+sdsModel.holdtime);

                    // string insertEvent = @"insert into [PhoneSign].[dbo].[P_Events] (e_EventNum, e_RoomId, e_SSID, e_RcTime, e_TimeThresHold, e_TimeRange, e_TaskNum,e_ClassId,E_Status)values('" + EventNum + "'," + se.RoomID + ",'" + string.Join(",", terlist.Select(u => u.termenal).ToArray()) + "','" + se.Rctime + "'," + sdsModel.holdtime + ",'" + timehold + "','" + taskNumId + "',"+ classId + "," + se.EvenStatus + ")";

                    bool bol = se.EvenStatus == 1;

                    stDAL.insertEven(taskNumModel, se , StuList, terlist, sdsModel,timehold);

                    if (StuList.Count > 0)
                    {
                        //stDAL.updateIsHandle(1, 3, tnModel.taskNumId, sel[0].Stlist[sel[0].Stlist.Count - 1].ScanTime);
                        //stDAL.updateIsHandle(1, 2, taskNumModel.taskNumId, se.Stlist[se.Stlist.Count-1].ScanTime);
                       stDAL.updateIsHandle(1, 2, taskNumModel.taskNumId, CurrentTime);
                    }

                }
            }
            return se;
            #endregion
        }
    }
}